package cn.softwin.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

public class JDBCpoolUtils {
	private static DataSource ds=null;
	static {
		try {
			Properties pro =new Properties();
//			ClassLoader cl=JDBCUtilsSJC.class.getClassLoader();
//			InputStream is= cl.getResourceAsStream("druid.properties");
			pro.load(JDBCpoolUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
			ds = DruidDataSourceFactory.createDataSource(pro);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获取连接对象
	public static Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
	//增删改的关流
	public static void close(Statement stat,Connection con) {
		if(stat!=null) {
			try {
				stat.close();//这里是关闭
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();//归还只是针对数据库连接池的Connection方法
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		close(null,stat,con);
	}
	//查询的关流
	public static void close(ResultSet res,Statement stat,Connection con) {
		if(res!=null) {
			try {
				res.close();//这里是关闭
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stat!=null) {
			try {
				stat.close();//这里是关闭
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();//归还只是针对数据库连接池的Connection方法的一种描述
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		close(stat,con);
	}
	//获取连接池的方法,返回DataSource,DataSource返回的是ds，ds就是调用连接池
	public static DataSource getDataSource() {
		return ds;
	}
}
